<template>
  <view>
    <view class="search-box">
      <my-search-bar placeholder="搜索" @myevent="getValue" />
    </view>
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular :interval="2000">
      <navigator
        :url="`/subpkg/goods-detail/index?goods_id=${item.goods_id}`"
        v-for="item in swiper"
        :key="item.goods_id"
      >
        <swiper-item>
          <image :src="item.image_src" mode="scaleToFill" />
        </swiper-item>
      </navigator>
    </swiper>

    <!-- 导航菜单 -->
    <view class="nav-list">
      <image
        class="nav-img"
        v-for="i in cartitem"
        :key="i.name"
        :src="i.image_src"
        mode="scaleToFill"
        @click="goToCategory(i.name)"
      />
    </view>
    <!-- 楼层 -->
    <view v-for="i in floors" :key="i.floor_title.name">
      <!-- 头部 -->
      <image
        class="floor-title"
        :src="i.floor_title.image_src"
        mode="scaleToFill"
      />
      <view class="floor-img-box">
        <!-- 左边 -->
        <navigator
          :url="i.product_list[0].navigator_url"
          open-type="navigate"
          hover-class="navigator-hover"
        >
          <image
            :src="i.product_list[0].image_src"
            :style="{ width: i.product_list[0].image_width + 'rpx' }"
            mode="widthFix"
          />
        </navigator>
        <!-- 右边 -->
        <view class="right-img-box">
          <navigator
            v-for="(sub, index) in i.product_list"
            v-show="index > 0"
            :key="sub.name"
            :url="sub.navigator_url"
          >
            <image
              :src="sub.image_src"
              :style="{ width: sub.image_width + 'rpx' }"
              mode="widthFix"
            />
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import tarBarBadge from '@/mixins/tarBar-badge.js'
export default {
  mixins: [tarBarBadge],
  data () {
    return {
      swiper: [],
      cartitem: [],
      floors: []
    }
  },
  onLoad () {
    this.getSwiper()
    this.getCartitem()
    this.getFloors()
  },
  methods: {
    // 轮播图
    async getSwiper () {
      const res = await uni.$request({
        url: 'home/swiperdata'
      })
      if (res.meta.status === 200) {
        console.log('首页轮播图', res)
        this.swiper = res.message
      }
    },
    // 导航菜单
    async getCartitem () {
      const res = await uni.$request({
        url: 'home/catitems'
      })
      console.log('首页菜单图', res)
      if (res.meta.status === 200) {
        this.cartitem = res.message
      }
    },
    // 跳到分类页面
    goToCategory (name) {
      if (name === '分类') {
        uni.switchTab({ url: '../category/index' })
      }
    },
    // 楼层
    async getFloors () {
      const res = await uni.$request({
        url: 'home/floordata'
      })
      console.log('首页楼层', res)
      if (res.meta.status === 200) {
        res.message.forEach(item => {
          item.product_list.forEach(subitem => {
            subitem.navigator_url = `/subpkg/goods-list/index?${
              subitem.navigator_url.split('?')[1]
            }`
          })
        })
        this.floors = res.message
      }
    },
    getValue (val) {
      console.log('---val---', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
